package com.enqualcomm.kids.mvp.seetheworld;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.enqualcomm.kids.R;
import com.enqualcomm.kids.activities.AQYPlayActivity_;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.manager.Controller;
import com.enqualcomm.kids.view.PullRefreshListView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import common.utils.GsonFactory;
import common.utils.MyLogger;
import in.srain.cube.views.GridViewWithHeaderAndFooter;

/**
 * Created by Administrator on 2016/3/11.
 */
public class AQYFragment extends Fragment implements AdapterView.OnItemClickListener {
    Activity activity;
    RequestQueue mQueue;
    AQYListViewAdapter adapter;
    List<AQYAlbum.Album> albumList;

    AppDefault appDefault;

    GridView gridView;

    private List<String> tvidsLists = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mQueue = Volley.newRequestQueue(getContext());
        appDefault = new AppDefault();
        getChildCategory();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_aiqiyi, null);
        adapter = new AQYListViewAdapter(getContext());
        gridView = (GridView) view.findViewById(R.id.ly_image_list_grid);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);
        return view;
    }

    private void getChildCategory() {
        String requestUrl;
        if( Controller.getInstance().isOldMainMode()){
//            requestUrl = "http://expand.video.iqiyi.com/api/album/list.json?apiKey=a813b5c475d74730b517748d713bab4d&categoryId=32";
            requestUrl =   "http://expand.video.iqiyi.com/api/search/list.json?apiKey=a813b5c475d74730b517748d713bab4d&keyWord="+ URLEncoder.encode("老年人");
        }else {
            requestUrl = "http://expand.video.iqiyi.com/api/album/list.json?apiKey=a813b5c475d74730b517748d713bab4d&categoryId=15";
        }

        StringRequest stringRequest = new StringRequest(requestUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    AQYAlbum aqyAlbum = GsonFactory.newInstance().fromJson(response,AQYAlbum.class);
                    albumList = aqyAlbum.data;
                    if (albumList != null && albumList.size()!= 0){
//                        MyLogger.kLog().i("albumList:"+albumList.size());
                        adapter.setData(albumList);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                MyLogger.kLog().i(error);
            }
        }

        );
        mQueue.add(stringRequest);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        view.setSelected(true);
        Intent intent = new Intent(getActivity(), AQYPlayActivity_.class);
        intent.putExtra("url", albumList.get(position).html5PlayUrl);
        getActivity().startActivity(intent);
    }


    /*
    *    String filePath = getActivity().getExternalCacheDir()+"/jsonchild.txt";
                        File file = new File(filePath);
                        PrintStream ps = null;
                        try {
                            ps = new PrintStream(new FileOutputStream(file));
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        assert ps != null;
                        ps.println(response.toString());// 往文件里写入字符串
    * */




}
